package com.krtech.wecard.module.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.sys.entity.IdentityInfo;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.service.IdentityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/identity")
@Api(value = "身份管理",tags = "身份管理")
public class IdentityInfoController {

    @Autowired
    private IdentityInfoService identityInfoService;

    /**
     * 增
     */
    @PutMapping("/add")
    @ApiOperation(value = "新增")
    public IdentityInfo add(@RequestBody IdentityInfo identityInfo){
        if (StrUtil.isEmpty(identityInfo.getCode())){
            throw new BusinessException("必填参数code不能不为空");
        }
        SysUser userInfo = LoginUtil.getUserInfo();
        identityInfo.setCreatedTime(new Date());
        identityInfo.setCreatedBy(userInfo.getUserId().toString());
        identityInfoService.save(identityInfo);
        return identityInfo;
    }

    /**
     * 改
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新")
    public IdentityInfo update(@RequestBody IdentityInfo identityInfo){
        if (StrUtil.isEmpty(identityInfo.getCode())){
            throw new BusinessException("必填参数code不能不为空");
        }
        identityInfoService.updateById(identityInfo);
        return identityInfo;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    public Boolean delete( Integer id){
        if (id == null){
            throw new BusinessException("必填参数id不能不为空");
        }
        identityInfoService.removeById(id);
        return true;
    }

    /**
     * getById
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation(value = "getById")
    public IdentityInfo getById(Integer id){
        if (id == null){
            throw new BusinessException("必填参数id不能不为空");
        }

        return identityInfoService.getById(id);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Page<IdentityInfo> page(Page<IdentityInfo> page, String keyword){
        QueryWrapper<IdentityInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(IdentityInfo.COL_DEL_FLAG,0);
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(IdentityInfo.COL_NAME,keyword)
                    .or()
                    .like(IdentityInfo.COL_CODE,keyword);
        }
        Page<IdentityInfo> managementPage = identityInfoService.page(page, wrapper);
        return managementPage;
    }

    @GetMapping("/list")
    @ApiOperation(value = "list")
    public List<IdentityInfo> list(String keyword){
        QueryWrapper<IdentityInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(IdentityInfo.COL_DEL_FLAG,0);
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(IdentityInfo.COL_NAME,keyword)
                    .or()
                    .like(IdentityInfo.COL_CODE,keyword);
        }
        List<IdentityInfo> list = identityInfoService.list(wrapper);
        return list;
    }


}
